package com.kaiwukj.android.ufamily.hx;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.hyphenate.easeui.widget.presenter.EaseChatRowPresenter;
import com.hyphenate.util.EasyUtils;
import com.kaiwukj.android.mcas.utils.UToast;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.Constant;
import com.kaiwukj.android.ufamily.exception.AppException;
import com.kaiwukj.android.ufamily.hx.domain.RobotUser;
import com.kaiwukj.android.ufamily.mvp.ui.activity.GroupDetailsActivity;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.MainActivity;
import com.kaiwukj.android.ufamily.utils.VideoUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * you can new an EaseChatFragment to use or you can inherit it to expand.
 * You need call setArguments to pass chatType and userId
 * <br/>
 * <br/>
 * you can see ChatActivity in demo for your reference
 */
public class HxEaseChatFragment extends EaseChatFragment implements EaseChatFragment.EaseChatFragmentHelper {

    // constant start from 11 to avoid conflict with constant in base class
    private static final int ITEM_VIDEO = 11;
    private static final int ITEM_FILE = 12;
    private static final int ITEM_VOICE_CALL = 13;
    private static final int ITEM_VIDEO_CALL = 14;
    private static final int ITEM_CONFERENCE_CALL = 15;
    private static final int ITEM_LIVE = 16;

    private static final int REQUEST_CODE_SELECT_VIDEO = 11;
    private static final int REQUEST_CODE_SELECT_FILE = 12;
    private static final int REQUEST_CODE_GROUP_DETAIL = 13;
    private static final int REQUEST_CODE_CONTEXT_MENU = 14;
    private static final int REQUEST_CODE_SELECT_AT_USER = 15;


    private static final int MESSAGE_TYPE_SENT_VOICE_CALL = 1;
    private static final int MESSAGE_TYPE_RECV_VOICE_CALL = 2;
    private static final int MESSAGE_TYPE_SENT_VIDEO_CALL = 3;
    private static final int MESSAGE_TYPE_RECV_VIDEO_CALL = 4;
    private static final int MESSAGE_TYPE_CONFERENCE_INVITE = 5;
    private static final int MESSAGE_TYPE_LIVE_INVITE = 6;
    private static final int MESSAGE_TYPE_RECALL = 9;

    public static final long MAX_VIDEO_LENGTH = 10 * 1024 * 1024;

    private String groupId;
    private String titleName;
    private int groupType;
    private boolean isButler;

    private QMUITipDialog loadingDialog;
    private CompositeDisposable mCompositeDisposable;

    /**
     * if it is chatBot
     */
    private boolean isRobot;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState,
                DemoHelper.getInstance().getModel().isMsgRoaming() && (chatType != EaseConstant.CHATTYPE_CHATROOM));
    }

    @Override
    protected boolean turnOnTyping() {
        return DemoHelper.getInstance().getModel().isShowMsgTyping();
    }

    @Override
    protected void setUpView() {
        groupId = fragmentArgs.getString(Constant.EXTRA_USER_ID);
        titleName = fragmentArgs.getString(Constant.EXTRA_USER_NAME);
        groupType = fragmentArgs.getInt(Constant.EXTRA_GROUP_TYPE);
        isButler = fragmentArgs.getBoolean(Constant.EXTRA_IS_BUTLER);

        //LogUtils.d("titleName--------------------------->" + titleName);

//        跳转聊天成员界面
//        titleBar = (EaseTitleBar) getView().findViewById(R.id.title_bar);
//        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), ChaterListActivity.class);
//                intent.putExtra("groupType", groupType);
//                intent.putExtra("title", titleName);
//                intent.putExtra("groupId", groupId);
//                startActivity(intent);
//            }
//        });

        setChatFragmentHelper(this);
        if (chatType == Constant.CHATTYPE_SINGLE) {
            Map<String, RobotUser> robotMap = DemoHelper.getInstance().getRobotList();
            if (robotMap != null && robotMap.containsKey(toChatUsername)) {
                isRobot = true;
            }
        }
        super.setUpView();
        // set click listener
        titleBar.setLeftLayoutClickListener(v -> {
            if (EasyUtils.isSingleActivity(getActivity())) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
            onBackPressed();
        });
        if (chatType == EaseConstant.CHATTYPE_GROUP) {
            inputMenu.getPrimaryMenu().getEditText().addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (count == 1 && "@".equals(String.valueOf(s.charAt(start)))) {
//                        startActivityForResult(new Intent(getActivity(), PickAtUserActivity.class).
//                                putExtra("groupId", toChatUsername), REQUEST_CODE_SELECT_AT_USER);
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    }

    private void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    private void cancelDisposable() {
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    protected void showLoading(String loadingMsg) {
        if (loadingDialog == null) {
            loadingDialog = new QMUITipDialog.Builder(getActivity())
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(loadingMsg)
                    .create();
            loadingDialog.setCanceledOnTouchOutside(false);
            loadingDialog.setCancelable(true);
        }
        loadingDialog.show();
    }

    protected void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.cancel();
        }
    }

    @Override
    protected void registerExtendMenuItem() {
        //use the menu in base class
        super.registerExtendMenuItem();
        //extend menu items
//        inputMenu.registerExtendMenuItem(R.string.attach_video, R.drawable.em_chat_video_selector, ITEM_VIDEO, extendMenuItemClickListener);
//        inputMenu.registerExtendMenuItem(R.string.attach_file, R.drawable.em_chat_file_selector, ITEM_FILE, extendMenuItemClickListener);
//        if(chatType == Constant.CHATTYPE_SINGLE){
//            inputMenu.registerExtendMenuItem(R.string.attach_voice_call, R.drawable.em_chat_voice_call_selector, ITEM_VOICE_CALL, extendMenuItemClickListener);
//            inputMenu.registerExtendMenuItem(R.string.attach_video_call, R.drawable.em_chat_video_call_selector, ITEM_VIDEO_CALL, extendMenuItemClickListener);
//        } else if (chatType == Constant.CHATTYPE_GROUP) { // 音视频会议
//            inputMenu.registerExtendMenuItem(R.string.voice_and_video_conference, R.drawable.em_chat_video_call_selector, ITEM_CONFERENCE_CALL, extendMenuItemClickListener);
//            inputMenu.registerExtendMenuItem(R.string.title_live, R.drawable.em_chat_video_call_selector, ITEM_LIVE, extendMenuItemClickListener);
//        }
    }


    @Override
    public void onSetMessageAttributes(EMMessage message) {
//        if (isRobot) {
//            //set message extension
//            message.setAttribute("em_robot_message", isRobot);
//        }
        // 通过扩展属性，将userPic和userName发送出去。
     /*   String userPic = SPUtils.getInstance().getString(SPParam.HX_HEAD_IMAGE);
        if (!TextUtils.isEmpty(userPic)) {
            message.setAttribute("avatar", userPic);
        }
        String userName = SPUtils.getInstance().getString(SPParam.HX_USER_NICK_NAME);
        if (!TextUtils.isEmpty(userName)) {
            message.setAttribute("userName", userName);
        }
        String userId = SPUtils.getInstance().getString(SPParam.HX_USER_ID);
        if (!TextUtils.isEmpty(userId)) {
            message.setAttribute("userId", userId);
        }*/
        if (isButler) {
            message.setAttribute("isButler", true);
        }

        // UserCacheManager.setMsgExt(message);

        if (chatType == EaseConstant.CHATTYPE_GROUP) {
            if (!TextUtils.isEmpty(titleName)) {
                message.setAttribute("groupName", titleName);
            }

            if (groupType != -1) {
                message.setAttribute("groupType", groupType);
            }

            if (!TextUtils.isEmpty(groupId)) {
                message.setAttribute("groupId", groupId);
            }
////            LogUtils.i("groupName0___" + titleName);
////            LogUtils.i("groupType0___" + groupType);
////            LogUtils.i("groupId0___" + groupId);
        }
        UserCacheManager.setMsgExt(message);
    }

    @Override
    public void onEnterToChatDetails() {
        if (chatType == Constant.CHATTYPE_GROUP) {
            EMGroup group = EMClient.getInstance().groupManager().getGroup(toChatUsername);
            if (group == null) {
                Toast.makeText(getActivity(), R.string.gorup_not_found, Toast.LENGTH_SHORT).show();
                return;
            }
            startActivityForResult(
                    (new Intent(getActivity(), GroupDetailsActivity.class).putExtra("groupId", toChatUsername)),
                    REQUEST_CODE_GROUP_DETAIL);
        }
    }

    @Override
    public void onAvatarClick(String username) {
        // LogUtils.d("userName------------------->" + username);
        //LogUtils.d("userName------------------->" + GsonUtils.toJson(UserCacheManager.get(username)));
        UserCacheInfo userInfo = UserCacheManager.get(username);
        if (userInfo != null && userInfo.getuId() > 0) {
            ARouter.getInstance().build(ARouterUrlKt.DynamicPersonUrl)
                    .withInt("userId", userInfo.getuId()).navigation();
        }
    }

    @Override
    public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
        return new CustomChatRowProvider();
    }


    @Override
    public void onAvatarLongClick(String username) {
        inputAtUsername(username);
    }


    @Override
    public boolean onMessageBubbleClick(EMMessage message) {
        //消息框点击事件，demo这里不做覆盖，如需覆盖，return true
      /*  if (message.getType() == EMMessage.Type.VIDEO) {
            EMVideoMessageBody videoMessageBody = (EMVideoMessageBody) message.getBody();

            ARouter.getInstance().build(ARouterUrlKt.VideoPlay)
                    .withString("videoUrl", videoMessageBody.)
                    .withString("coverUrl", item.getImages())
                    .withString("title", item.getDynamicContent())
                    //.withString(ExtraCons.EXTRA_KEY_VIDEO_DATA, new Gson().toJson(new DynamicCollection(getData())))
                    //.withInt("noteId", item.getId())
                    //.withString(ExtraCons.EXTRA_KEY_EDIT_MINE, PersonHomePageFragment.PERSON_HOME_PAGE_FRAGMENT)
                    .navigation(getContext());
        });
        return true;
    }*/
        if (message.getType() == EMMessage.Type.VIDEO) {
            LogUtils.d("------------video click------------");
        }
        return false;
    }

    @Override
    public void onCmdMessageReceived(List<EMMessage> messages) {
        super.onCmdMessageReceived(messages);
    }

    @Override
    public void onMessageBubbleLongClick(EMMessage message) {
        // no message forward when in chat room

    }

    @Override
    public boolean onExtendMenuItemClick(int itemId, View view) {
        switch (itemId) {
            case ITEM_VIDEO:
                break;
            case ITEM_FILE: //file
                selectFileFromLocal();
                break;
            case ITEM_VOICE_CALL:
                break;
            case ITEM_VIDEO_CALL:
                break;

            default:
                break;
        }
        //keep exist extend menu
        return false;
    }

    /**
     * select file
     */
    protected void selectFileFromLocal() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        startActivityForResult(intent, REQUEST_CODE_SELECT_FILE);
    }

    @Override
    protected void sendVideoMessage(String videoPath, String thumbPath, int videoLength) {
        // super.sendVideoMessage(videoPath, thumbPath, videoLength);
        if (FileUtils.getFileLength(videoPath) > 8 * MAX_VIDEO_LENGTH) {
            UToast.showShort(getContext(), "视频文件不能超过80M");
            return;
        }
        if (FileUtils.getFileLength(videoPath) > MAX_VIDEO_LENGTH) {
            showLoading("正在处理...");
            Disposable disposable = Observable.create((ObservableOnSubscribe<String>) emitter -> {
                VideoUtils.getInstance().startCompressed(getContext(), videoPath, -1, new VideoUtils.OnVideoDealListener() {
                    @Override
                    public void onSuccess(String filePath) {
                        emitter.onNext(filePath);
                    }

                    @Override
                    public void onFailed(int errorCode, String errorMsg) {
                        // showMessage(errorCode + ":" + errorMsg);
                        emitter.onError(new AppException(errorCode, errorMsg));
                    }

                    @Override
                    public void onProgressUpdate(float progress) {
                    }
                });
            })
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(result -> {
                        hideLoading();
                        if (FileUtils.getFileLength(result) > MAX_VIDEO_LENGTH) {
                            UToast.showShort(getContext(), "视频文件过大!!!");
                        } else {
                            super.sendVideoMessage(result, thumbPath, videoLength);
                        }
                    }, error -> {
                        hideLoading();
                        if (error instanceof AppException) {
                            if (((AppException) error).getErrCode() == 8 || ((AppException) error).getErrCode() == 9) {
                                //  mPostCardAdapter.setList(result);
                                UToast.showShort(getContext(), "视频文件过大");
                                return;
                            }
                        }
                        UToast.showShort(getActivity(), Objects.requireNonNull(error.getMessage()));
                        // showMessage(Objects.requireNonNull(error.getMessage()));

                    });
            addDisposable(disposable);
        } else {
            super.sendVideoMessage(videoPath, thumbPath, videoLength);
        }
    }

    /**
     * chat row provider
     */
    private final class CustomChatRowProvider implements EaseCustomChatRowProvider {
        @Override
        public int getCustomChatRowTypeCount() {
            //here the number is the message type in EMMessage::Type
            //which is used to count the number of different chat row
            return 14;
        }

        @Override
        public int getCustomChatRowType(EMMessage message) {
            if (message.getType() == EMMessage.Type.TXT) {
                //voice call
                if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE_CALL : MESSAGE_TYPE_SENT_VOICE_CALL;
                } else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)) {
                    //video call
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO_CALL : MESSAGE_TYPE_SENT_VIDEO_CALL;
                }
                //messagee recall
                else if (message.getBooleanAttribute(Constant.MESSAGE_TYPE_RECALL, false)) {
                    return MESSAGE_TYPE_RECALL;
                } else if (!"".equals(message.getStringAttribute(Constant.MSG_ATTR_CONF_ID, ""))) {
                    return MESSAGE_TYPE_CONFERENCE_INVITE;
                } else if (Constant.OP_INVITE.equals(message.getStringAttribute(Constant.EM_CONFERENCE_OP, ""))) {
                    return MESSAGE_TYPE_LIVE_INVITE;
                }
            }
            return 0;
        }

        @Override
        public EaseChatRowPresenter getCustomChatRow(EMMessage message, int position, BaseAdapter adapter) {
//            if(message.getType() == EMMessage.Type.TXT){
//                // voice call or video call
//                if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false) ||
//                        message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)){
//                    EaseChatRowPresenter presenter = new EaseChatVoiceCallPresenter();
//                    return presenter;
//                }
//                //recall message
//                else if(message.getBooleanAttribute(Constant.MESSAGE_TYPE_RECALL, false)){
//                    EaseChatRowPresenter presenter = new EaseChatRecallPresenter();
//                    return presenter;
//                } else if (!"".equals(message.getStringAttribute(Constant.MSG_ATTR_CONF_ID,""))) {
//                    return new ChatRowConferenceInvitePresenter();
//                } else if (Constant.OP_INVITE.equals(message.getStringAttribute(Constant.EM_CONFERENCE_OP, ""))) {
//                    return new ChatRowLivePresenter();
//                }
//            }
            return null;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        hideLoading();
        cancelDisposable();
    }
}
